package com.ruoyi.sql;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.util.ReflectUtil;

import java.lang.reflect.Field;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * **********************
 * * Author: XiaShiLong *
 * * Date  : 2021-05-14 *
 * * Time  : 13:31      *
 * * to    : lz&xm      *
 * **********************
 **/
public class ColumnUtil {

    public static String getAllColumn(Class<?> classes) {
        Field[] allField = ReflectUtil.getField(classes);
        return Stream.of(allField)
                .filter(ColumnUtil::filter)
                .map(f -> StringUtils.camelToUnderline(f.getName()))
                .collect(Collectors.joining(","));
    }

    private static boolean filter(Field field) {
        TableField annotation = field.getAnnotation(TableField.class);
        return !field.getName().equals("serialVersionUID") && (Objects.isNull(annotation) || annotation.exist());
    }
}
